﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Maticsoft.DBUtility;

namespace BBPA.DAL
{
    public class GroupBuy
    {
        public GroupBuy()
        { }
        
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int gbid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from GroupBuy");
            strSql.Append(" where gbid=@gbid");
            SqlParameter[] parameters = {
					new SqlParameter("@gbid", SqlDbType.Int,4)
			};
            parameters[0].Value = gbid;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(BBPA.Model.GroupBuy model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into GroupBuy(");
            strSql.Append("gbname,gbprice,gbendtime,gbcontent,gbsupplierid,gbstatus,gborderprice,gbpayprice,gbcreatetime)");
            strSql.Append(" values (");
            strSql.Append("@gbname,@gbprice,@gbendtime,@gbcontent,@gbsupplierid,@gbstatus,@gborderprice,@gbpayprice,GETDATE())");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@gbname", SqlDbType.NVarChar,50),
					new SqlParameter("@gbprice", SqlDbType.Money,8),
					new SqlParameter("@gbendtime", SqlDbType.DateTime),
					new SqlParameter("@gbcontent", SqlDbType.NVarChar,500),
					new SqlParameter("@gbsupplierid", SqlDbType.Int,4),
					new SqlParameter("@gbstatus", SqlDbType.Int,4),
					new SqlParameter("@gborderprice", SqlDbType.Money,8),
					new SqlParameter("@gbpayprice", SqlDbType.Money,8),
					new SqlParameter("@gbcreatetime", SqlDbType.DateTime)};
            parameters[0].Value = model.gbname;
            parameters[1].Value = model.gbprice;
            parameters[2].Value = model.gbendtime;
            parameters[3].Value = model.gbcontent;
            parameters[4].Value = model.gbsupplierid;
            parameters[5].Value = model.gbstatus;
            parameters[6].Value = model.gborderprice;
            parameters[7].Value = model.gbpayprice;
            //parameters[8].Value = model.gbcreatetime;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(BBPA.Model.GroupBuy model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update GroupBuy set ");
            strSql.Append("gbname=@gbname,");
            strSql.Append("gbprice=@gbprice,");
            strSql.Append("gbendtime=@gbendtime,");
            strSql.Append("gbcontent=@gbcontent,");
            strSql.Append("gbsupplierid=@gbsupplierid,");
            strSql.Append("gbstatus=@gbstatus,");
            strSql.Append("gborderprice=@gborderprice,");
            strSql.Append("gbpayprice=@gbpayprice");
            //strSql.Append("gbcreatetime=@gbcreatetime");
            strSql.Append(" where gbid=@gbid");
            SqlParameter[] parameters = {
					new SqlParameter("@gbname", SqlDbType.NVarChar,50),
					new SqlParameter("@gbprice", SqlDbType.Money,8),
					new SqlParameter("@gbendtime", SqlDbType.DateTime),
					new SqlParameter("@gbcontent", SqlDbType.NVarChar,500),
					new SqlParameter("@gbsupplierid", SqlDbType.Int,4),
					new SqlParameter("@gbstatus", SqlDbType.Int,4),
					new SqlParameter("@gborderprice", SqlDbType.Money,8),
					new SqlParameter("@gbpayprice", SqlDbType.Money,8),
					//new SqlParameter("@gbcreatetime", SqlDbType.DateTime),
					new SqlParameter("@gbid", SqlDbType.Int,4)};
            parameters[0].Value = model.gbname;
            parameters[1].Value = model.gbprice;
            parameters[2].Value = model.gbendtime;
            parameters[3].Value = model.gbcontent;
            parameters[4].Value = model.gbsupplierid;
            parameters[5].Value = model.gbstatus;
            parameters[6].Value = model.gborderprice;
            parameters[7].Value = model.gbpayprice;
            //parameters[8].Value = model.gbcreatetime;
            parameters[8].Value = model.gbid;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int gbid)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from GroupBuy ");
            strSql.Append(" where gbid=@gbid");
            SqlParameter[] parameters = {
					new SqlParameter("@gbid", SqlDbType.Int,4)
			};
            parameters[0].Value = gbid;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string gbidlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from GroupBuy ");
            strSql.Append(" where gbid in (" + gbidlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public BBPA.Model.GroupBuy GetModel(int gbid)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 gbid,gbname,gbprice,gbendtime,gbcontent,gbsupplierid,gbstatus,gborderprice,gbpayprice,gbcreatetime from GroupBuy ");
            strSql.Append(" where gbid=@gbid");
            SqlParameter[] parameters = {
					new SqlParameter("@gbid", SqlDbType.Int,4)
			};
            parameters[0].Value = gbid;

            BBPA.Model.GroupBuy model = new BBPA.Model.GroupBuy();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public BBPA.Model.GroupBuy DataRowToModel(DataRow row)
        {
            BBPA.Model.GroupBuy model = new BBPA.Model.GroupBuy();
            if (row != null)
            {
                if (row["gbid"] != null && row["gbid"].ToString() != "")
                {
                    model.gbid = int.Parse(row["gbid"].ToString());
                }
                if (row["gbname"] != null)
                {
                    model.gbname = row["gbname"].ToString();
                }
                if (row["gbprice"] != null && row["gbprice"].ToString() != "")
                {
                    model.gbprice = decimal.Parse(row["gbprice"].ToString());
                }
                if (row["gbendtime"] != null && row["gbendtime"].ToString() != "")
                {
                    model.gbendtime = DateTime.Parse(row["gbendtime"].ToString());
                }
                if (row["gbcontent"] != null)
                {
                    model.gbcontent = row["gbcontent"].ToString();
                }
                if (row["gbsupplierid"] != null && row["gbsupplierid"].ToString() != "")
                {
                    model.gbsupplierid = int.Parse(row["gbsupplierid"].ToString());
                }
                if (row["gbstatus"] != null && row["gbstatus"].ToString() != "")
                {
                    model.gbstatus = int.Parse(row["gbstatus"].ToString());
                }
                if (row["gborderprice"] != null && row["gborderprice"].ToString() != "")
                {
                    model.gborderprice = decimal.Parse(row["gborderprice"].ToString());
                }
                if (row["gbpayprice"] != null && row["gbpayprice"].ToString() != "")
                {
                    model.gbpayprice = decimal.Parse(row["gbpayprice"].ToString());
                }
                if (row["gbcreatetime"] != null && row["gbcreatetime"].ToString() != "")
                {
                    model.gbcreatetime = DateTime.Parse(row["gbcreatetime"].ToString());
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select gbid,gbname,gbprice,gbendtime,gbcontent,gbsupplierid,gbstatus,gborderprice,gbpayprice,gbcreatetime ");
            strSql.Append(" FROM GroupBuy ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" gbid,gbname,gbprice,gbendtime,gbcontent,gbsupplierid,gbstatus,gborderprice,gbpayprice,gbcreatetime ");
            strSql.Append(" FROM GroupBuy ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM GroupBuy ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.gbid desc");
            }
            strSql.Append(")AS Row, T.*  from GroupBuy T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage_order(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.gbid desc");
            }
            strSql.Append(")AS Row, T.*  from V_OrderVSGroupby T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "GroupBuy";
            parameters[1].Value = "gbid";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        public void Plan_CheckGroupBuy()
        {
            DbHelperSQL.RunProcedure("DEL_OUTGROUPBY_ORDER", new SqlParameter[] { });
        }


        public DataTable GetOutGroupBuy()
        {
            StringBuilder strsql = new StringBuilder();
            strsql.Append(" update GroupBuy set gbstatus=3 where DATEDIFF(DAY,gbendtime,GETDATE())>=1 and gbpayprice<gbprice and gbstatus=1 \r\n ");
            strsql.Append("select * from GroupBuy g left join Orders o on g.gbid=o.gbid ");
            strsql.Append(" left join Store s on s.id=o.StoreID");
            strsql.Append(" where  gbstatus not in(1,2) and o.Number is not null  ");
            return DbHelperSQL.Query(strsql.ToString()).Tables[0];
        }

        public void DelOrders_Goods(int id)
        {
            DbHelperSQL.ExecuteSql("delete from Orders_Goods where  OrdersID=" + id.ToString());
        }
    }
    
}
